// pages/home/device/acs01.js
var dateTimePicker = require('../../../utils/dateTimePicker.js');
var util = require('../../../utils/util.js');

var app = getApp();

var page_name = "";
var page_did = "";
var page_index = 0;
var page_size = 20;
var page_refresh = false;
var page_timer = null; 
var page_total = 20;
var query_size = 20;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    start_dateTime: null,
    start_dateTimeArray: null,
    end_dateTime: null,
    end_dateTimeArray: null,

    startYear: 1970,
    endYear: 2099,

    q_start:"",
    q_end:"",

    pagetitle:"设备数据",
    set_enable: true,
    history: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    page_timer = null;
    //console.log(options);
    page_did = options.did;
    page_name = options.name;
    //console.log("page_did:" + page_did)
    wx.setNavigationBarTitle({
      title: page_name//页面标题为路由参数
    })

    var dt=new Date();
    var ndt = new Date();
    ndt.setDate(ndt.getDate()-1);
    var start = util.formatTime(ndt);
    var end = util.formatTime(dt);
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj_start = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear, start);
    var obj_end = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear, end);

    this.setData({
      start_dateTime: obj_start.dateTime,
      start_dateTimeArray: obj_start.dateTimeArray,
      end_dateTime: obj_end.dateTime,
      end_dateTimeArray: obj_end.dateTimeArray,
    });

    this.getSelectTime();
    this.setData({
      q_start: start,
      q_end: end,
    });
  },
  //Start
  startChange(e) {
    this.setData({ start_dateTime: e.detail.value });
  },
  startChangeColumn(e) {
    var arr = this.data.start_dateTime, dateArr = this.data.start_dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      start_dateTimeArray: dateArr,
      start_dateTime: arr
    });

    this.getSelectTime();
  },
  //End
  endChange(e) {
    this.setData({ end_dateTime: e.detail.value });
  },
  endChangeColumn(e) {
    var arr = this.data.end_dateTime, dateArr = this.data.end_dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      end_dateTimeArray: dateArr,
      end_dateTime: arr
    });

    this.getSelectTime();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var token = app.globalData.Token;//app.globalData.Token;
    if (token == null || token == "") {
      wx.navigateTo({
        url: '/pages/user/login'
      })
      return;
    }
    page_timer = null;
    page_refresh = true;
    this.gethistory();
    this.ontimer();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearTimeout(page_timer);
    page_refresh = false;
    page_timer = null;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(page_timer);
    page_refresh = false;
    page_timer = null;
  },

  //获取设备报警
  gethistory: function (e) {
    //  该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
    //console.log("gethistory");
    var that = this;
    var token = app.globalData.Token;
    if (token == null || token == "") return;
    var page_start = that.data.q_start;
    var page_end = that.data.q_end;
    wx.request({
      url: app.globalData.Url + "Api/Device/GetHistory", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did,
        cmd: 'Device/History',
        start: page_start,
        end: page_end,
        index: page_index,
        size: query_size,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        //console.log(res.data);
        page_total = res.data.data.length;
        that.setData({ history: res.data.data });
        //console.log("page_total:" + page_total);
      }
    })
  },
  //定时方法
  ontimer: function () {
    var that = this;
    page_timer = setTimeout(function () {
      //console.log("----ontimer 定时器...----");
      if (page_refresh) {
        that.gethistory();
        that.ontimer();
      }
    }, 10000);
  },
  //查询
  onquery: function () {
    this.gethistory();
  },

  getSelectTime:function(){
    var that = this;

    var dt_s = "";
    dt_s += that.data.start_dateTimeArray[0][that.data.start_dateTime[0]] + "-";
    dt_s += that.data.start_dateTimeArray[1][that.data.start_dateTime[1]] + "-";
    dt_s += that.data.start_dateTimeArray[2][that.data.start_dateTime[2]] + " ";
    dt_s += that.data.start_dateTimeArray[3][that.data.start_dateTime[3]] + ":";
    dt_s += that.data.start_dateTimeArray[4][that.data.start_dateTime[4]] + ":";
    dt_s += that.data.start_dateTimeArray[5][that.data.start_dateTime[5]];

    var dt_e = "";
    dt_e += that.data.end_dateTimeArray[0][that.data.end_dateTime[0]] + "-";
    dt_e += that.data.end_dateTimeArray[1][that.data.end_dateTime[1]] + "-";
    dt_e += that.data.end_dateTimeArray[2][that.data.end_dateTime[2]] + " ";
    dt_e += that.data.end_dateTimeArray[3][that.data.end_dateTime[3]] + ":";
    dt_e += that.data.end_dateTimeArray[4][that.data.end_dateTime[4]] + ":";
    dt_e += that.data.end_dateTimeArray[5][that.data.end_dateTime[5]];


    this.setData({
      q_start: dt_s,
      q_end: dt_e,
    });
  },
/**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //this.data.hidden = false;
    //console.log("上拉触底事件");
    if (page_total >= query_size) {
      query_size = query_size+ page_size;
    }
    //console.log("page_total:" + page_total + " query_size:" + query_size);
    this.gethistory();
  },
})